package cn.wolfcode.wolf2w.redis.service.impl;

import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.StrategyComment;
import cn.wolfcode.wolf2w.redis.service.IStrategyStatisVORedisService;
import cn.wolfcode.wolf2w.redis.util.RedisKeys;
import cn.wolfcode.wolf2w.redis.vo.StrategyStatisVO;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.util.DateUtil;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.concurrent.TimeUnit;

@Service
public class StrategyStatisVORedisServiceImpl implements IStrategyStatisVORedisService {

    @Autowired
    private StringRedisTemplate template;
    @Autowired
    private IStrategyService strategyService;

    @Override
    public void viewnumIncrease(String sid, int num) {

        String key = RedisKeys.STRATEGY_STATIS_VO.join(sid);
        //判断vo对象是否存在
        StrategyStatisVO vo = null;
        if (template.hasKey(key)){
            String strvo = template.opsForValue().get(key);
            vo = JSON.parseObject(strvo,StrategyStatisVO.class);
        }else {
            //如果不存在，先初始化
            vo = new StrategyStatisVO();
            Strategy strategy = strategyService.get(sid);
            BeanUtils.copyProperties(strategy,vo);
            vo.setStrategyId(strategy.getId());
        }
        //点击量加1
        vo.setViewnum(vo.getViewnum() + num);
        //更新
        template.opsForValue().set(key,JSON.toJSONString(vo));
    }

    @Override
    public StrategyStatisVO getStrategyStatisVO(String sid) {

        String key = RedisKeys.STRATEGY_STATIS_VO.join(sid);

        StrategyStatisVO vo = null;
        if (template.hasKey(key)){
            String strvo = template.opsForValue().get(key);
            vo = JSON.parseObject(strvo,StrategyStatisVO.class);
        }else {
            //如果不存在，先初始化
            vo = new StrategyStatisVO();
            Strategy strategy = strategyService.get(sid);
            BeanUtils.copyProperties(strategy,vo);
            vo.setStrategyId(strategy.getId());
        }
        return vo;
    }

    @Override
    public void setStrategyStatisVO(StrategyStatisVO vo) {
        String key = RedisKeys.STRATEGY_STATIS_VO.join(vo.getStrategyId());
        template.opsForValue().set(key,JSON.toJSONString(vo));
    }

    @Override
    public void replynumIncrease(String sid, int num) {
        StrategyStatisVO vo = this.getStrategyStatisVO(sid);
        vo.setReplynum(vo.getReplynum()+num);
        this.setStrategyStatisVO(vo);
    }

    @Override
    public boolean favor(String sid, String id) {
        //攻略id集合作为key
        String key = RedisKeys.STRATRGY_FAVOR.join(sid);
        List<String> list = new ArrayList<>();
        //判断是否有key
        if (template.hasKey(key)){
            //获取key
            String listSid = template.opsForValue().get(key);
            list = JSON.parseArray(listSid,String.class);
        }
        //获取vo对象
        StrategyStatisVO vo = this.getStrategyStatisVO(sid);
        if (list.contains(sid)){
            vo.setFavornum(vo.getFavornum() - 1);
            list.remove(sid);
        }else {
            vo.setFavornum(vo.getFavornum() + 1);
            list.add(sid);
        }

        //更新list 集合和vo对象
        template.opsForValue().set(key,JSON.toJSONString(list));
        this.setStrategyStatisVO(vo);

        return false;
    }

    @Override
    public List<String> getUserStrategyFavor(String userId) {
        //没有登录，
        if (!StringUtils.hasLength(userId)){
            return Collections.EMPTY_LIST;
        }
        List<String> userIdList = new ArrayList<>();
        //通过用户ID获取key
        String key = RedisKeys.STRATRGY_FAVOR.join(userId);
        //判断是否有key
        if (template.hasKey(key)){
            String userIdStr = template.opsForValue().get(key);
            userIdList = JSON.parseArray(userIdStr,String.class);
        }
        return userIdList;
    }

    @Override
    public boolean strategyThumbup(String sid, String uid) {
        //攻略置顶标记作为key
        String key = RedisKeys.STRATRGY_THUMBUP.join(uid);
        //如果key不存在，表示今天可以置顶
        if (!template.hasKey(key)){
            StrategyStatisVO vo = this.getStrategyStatisVO(sid);
            vo.setThumbsupnum(vo.getThumbsupnum() + 1);
            //更新
            this.setStrategyStatisVO(vo);
            Date now = new Date();
            Date end = DateUtil.getEndDate(now);
            long time = DateUtil.getDateBetween(now, end);
            template.opsForValue().set(key,"1",time, TimeUnit.SECONDS);
            //顶成功
            return true;
        }

        return false;
    }

    @Override
    public boolean isStrategyVOExists(String sid) {
        String key = RedisKeys.STRATEGY_VO.join(sid);
        return template.hasKey(key);
    }

    @Override
    public List<StrategyStatisVO> listStrategyVOByPattern(String pattern) {
        Set<String> keys = template.keys(pattern);
        List<StrategyStatisVO> list = new ArrayList<>();
        if (keys !=null && keys.size()>0){
            for (String key : keys){
                String voStr = template.opsForValue().get(key);
                list.add(JSON.parseObject(voStr,StrategyStatisVO.class));
            }
        }

        return list;
    }


}
